/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import model.pojo.Disciplina;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

/**
 *
 * @author Pedro
 */
public class Arquivo{
    private Path patch = Paths.get("C:/Users/leonidia/Documents/NetBeansProjects/Trabalho_pratico/Arquivo/Diciplinas.txt");
    private Charset utf8 = StandardCharsets.UTF_8;
    
    public void armazenarDisciplinas(ArrayList<Disciplina> disciplinas)throws IOException{
        try(BufferedWriter writer = Files.newBufferedWriter(patch, utf8)){
            int i;
            for (i=0; i < disciplinas.size();i++){
                writer.write(disciplinas.get(i).getCargaHoraria() 
                        + ";" + disciplinas.get(i).getNome() 
                        + ";" + disciplinas.get(i).getEmenta()+"\n");
            }
        }
    }
    
    public ArrayList<Disciplina> recuperarDisciplinas()throws IOException{
        try (BufferedReader reader = Files.newBufferedReader(patch, utf8) ){
            String line = null;
            ArrayList<Disciplina> lista = new ArrayList<>() ;
            while ((line = reader.readLine()) != null){
                String[] t = line.split(";");
                Disciplina disciplina = new Disciplina(Integer.parseInt(t[0]),t[1],t[2]);
                lista.add(disciplina);
            }
            return lista;
        }
    }
}